const {bfmetaSDK} = require('../util/util')
const userModel = require('../model/userModel')
// 获得用户地址
const userAddress = () => {
  return async (req, res, next) => {
    try {
      const keypair = await bfmetaSDK.bfchainSignUtil.createKeypair(req.body.words)
      const address = await bfmetaSDK.bfchainSignUtil.getAddressFromPublicKey(keypair.publicKey)
      const [user] = await userModel.getUserByAddress(address)
      if(!user.length){
        await userModel.addUser(address, req.body.words)
      }
      res.status(200).send(address)
    } catch (error) {
      next(error)
    }
  }
}


module.exports = {userAddress}